<?php

namespace App\Jobs\Email;

use App\Mail\SendMail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class SendMailJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $email;
    public $content;
    public $view;
    public $title;

    /**
     *
     * @param array $email 邮箱地址
     * @param array $content 传输内容
     * @param string $view 视图名
     * @param string $title 邮件主题
     */
    public function __construct($email = [], $content = [], $view = 'email', $title = '')
    {
        $this->email = $email;
        $this->view = $view;
        $this->title = $title;
        $this->content = $content;
    }


    public function handle()
    {
        foreach ($this->email as $v) {
            try {
                \Mail::send(new SendMail($v, $this->content, $this->view, $this->title));
                echo $v."：执行成功";
            } catch (\Exception $e) {
                echo '执行失败';
                \Log::channel('emailLog')->error($e);
            }
        }
    }

    public function fail($exception = null)
    {
        return [];
    }
}
